package com.haoran.dwz.initilizer;

import org.springframework.context.ApplicationContextInitializer;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Configuration;

import com.haoran.dwz.config.util.ConsulConfigurationSource;
import com.netflix.config.AbstractPollingScheduler;
import com.netflix.config.ConfigurationManager;
import com.netflix.config.DynamicConfiguration;
import com.netflix.config.FixedDelayPollingScheduler;
import com.netflix.config.PolledConfigurationSource;

@Configuration
public class ConsulPropertySourceInitilizer implements ApplicationContextInitializer<ConfigurableApplicationContext>{

	private String serviceName="dwz";
	
	private String serviceTag="dev";
	/**
	 * 在启动类里面增加如下调用
	 * <pre>
	 * 
	 * 	public static void main(String[] args){
	 * 		SpringApplication sa = new SpringApplication(DwzConsoleApplication.class);
	 *		sa.addInitializers(new ConsulPropertySourceInitilizer());
	 *		sa.run(args);
	 * 	}
	 * 
	 * </pre>
	 */
	@Override
	public void initialize(ConfigurableApplicationContext applicationContext) {
		System.setProperty("archaius.fixedDelayPollingScheduler.delayMills", "10000");		
		String keyName = "service/" + serviceName+"/"+serviceTag;
		applicationContext.getApplicationName();
		PolledConfigurationSource configSource = new ConsulConfigurationSource(keyName);
		AbstractPollingScheduler scheduler = new FixedDelayPollingScheduler();
		DynamicConfiguration configuration = new DynamicConfiguration(configSource, scheduler);
		ConfigurationManager.install(configuration);
	}

}
